<?php

function loadmodules() {
	//Check all necessary files exist and functions are defined

	if (!include_once('config.inc.php'))
		error("missing file 'config.inc.php'");
	else if (!include_once('functions.inc.php'))
		error("missing file 'functions.inc.php'");
	else if (!include_once('wikipedia.php'))
		error("missing file 'wikipedia.php'");
	else if (!include_once('flickr.php'))
		error("missing file 'flickr.php'");
	else if (!function_exists('get_related_terms'))
		error("missing function 'get_related_terms'");
	else if (!function_exists('get_images'))
		error("missing function 'get_related'");
}

function mainlogic() {
	$starttime = microtime(true);

	if( isset( $_GET[ "q" ] ))
		$search = $_GET[ "q" ];
	else
		$search = "ucla"; // Testing purposes

	//Alan: connect to local database
	$resource_local = connectToDatabase();

	$cache_hits = 0;
	//Alan: get related terms
	$terms = get_related_terms($resource_local, $search, $cache_hits );
// 	$terms = array( array( "ucla", null ), array( "kevin love", null ), 
// 			array( "usc", null) , array( "cal tech", null) ,
// 			array( "stanford", null ), array("vint cerf", null ), 
// 			array( "john wooden", null ), 
// 			array("kleinrock", null) );

	
	// Convert into a format that get_images() expects
// 	$strip_terms = array();
// 	foreach( $terms as $t )
// 	  $strip_terms[] = $t[ 0 ];
	$img_html = get_images( $terms, $search, 1 );

 	foreach( $img_html as $v ) {
 		$tag = $v["tag"];
 		$url = $v["url"];
 		$web = $v["web"];
		$context = $v["context"];


	    echo '<tr>
	        <td class="col_one">
		    <img src="'. $url  .'" />
		</td>
		<td class="col_two">
		    <div class="description">
		        <div class="term"><a href="?q='.$tag.'">'. 
	      $tag .'</a>';
	    echo '</div>
		        <div class="context">'. $context .'</div>
		    </div>';
	    echo "<a class=\"flickr_link\" href=\"". $web ."\" target=\"_blank\"><img src=\"flickr.png\" /></a>";

	    echo '</td>
	    </tr>
	    <tr class="spacer"><td></td><td></td></tr>';


/*		echo "\t\t\t\t";
 		echo '<div class="draggable" class="ui-widget-content" >'.
 		  '<img src="'. $url .'"><p>'. $tag .'</p></img>'.
		  '</div>'.
		  '<div class = "tooltip">'.
		  '<p>'. $tag  .'</p>'.
		  '<img src="'. $url  .'"></img>'.
		  '<p>Related terms: Westwood, Los Angeles, Basketball </p>'.
		  '<a href="'. $web .'">Flickr</a>'.
 		  '</div>';
*/

// 		echo "<li class = '1st draggable'>
//                                 <a ><img src=".$url." alt='' /> </a>
//                                 <ul>
//                                         <li class='searchterm'>".$tag."</li>
//                                         <li> <a href='$web'>Flickr</a> </li>
//                                         <li>\"$context\"</li>
//                                         <li><a href=".$url." rel='example1'> Lightbox </a></li>
//                                 </ul>
//                         </li>";


		  /******************NEW FORMAT******************/
// echo '			<ul class="thumb">
// 				<li class = "1st draggable"><a href="main_image1.jpg"><img src="1.jpg" alt="" /></a><ul><li>ucla_04.jpg</li>
// 				<li class="searchterm">UCLA </li><li>ust.ucla.edu</li> <li>Similar</li></ul></li>
// 				<li class = "1st draggable"><a href="main_image2.jpg"><img src="2.jpg" alt="" /></a><ul><li>UCLA</li><li>URL</li></ul></li>
// 				<li class = "1st drabble"><a href="main_image3.jpg"><img src="3.jpg" alt="" /></a><ul><li>UCLA</li><li>URL</li></ul></li>
// 				<li class = "1st draggable"><a href="main_image4.jpg"><img src="4.jpg" alt="" /></a><ul><li>UCLA</li><li>URL</li></ul></li>
// 			</ul>';



 		echo "\n";

// 		echo "\n\t\t\t\t<div class='draggable' class='ui-widget-content'>".
// 		"<img src='". $url ."'><p>". $tag ."</p></img></div>";
	}

	$latency = timespan_output($starttime, microtime(true), 'bottomOutput');

	if ($resource_local)
	{
		$remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost';
		metrics_log($resource_local, $remote_addr, $search, $cache_hits, round($latency * 1000));
	}

}

loadmodules();
mainlogic();

?>
